<?php
declare(strict_types=1);

namespace App\Listener;

use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Validation\Event\ValidatorFactoryResolved;

#[Listener]
class ValidatorFactoryResolvedListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            ValidatorFactoryResolved::class
        ];
    }

    public function process(object $event)
    {
        $validatorFactory = $event->validatorFactory;

        $validatorFactory->extend('color', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $value);
        });

        $validatorFactory->replacer('color', function ($message, $attribute, $rule, $paramters) {
            return str_replace(':color', $attribute, $message);
        });
    }
}